<?php require_once('../Connections/locale.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO abbonati (nominativo, riferimento, indirizzo, cap, id_citta, data_registrazione, id_categoria, telefono, cellulare, email, note) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['nominativo'], "text"),
                       GetSQLValueString($_POST['riferimento'], "text"),
                       GetSQLValueString($_POST['indirizzo'], "text"),
                       GetSQLValueString($_POST['cap'], "text"),
                       GetSQLValueString($_POST['id_citta'], "int"),
                       GetSQLValueString($_POST['data_registrazione'], "date"),
                       GetSQLValueString($_POST['id_categoria'], "int"),
                       GetSQLValueString($_POST['telefono'], "text"),
                       GetSQLValueString($_POST['cellulare'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['note'], "text"));

  mysql_select_db($database_locale, $locale);
  $Result1 = mysql_query($insertSQL, $locale) or die(mysql_error());

  $insertGoTo = "elenco_abbonati.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_locale, $locale);
$query_Categorie = "SELECT * FROM categorie";
$Categorie = mysql_query($query_Categorie, $locale) or die(mysql_error());
$row_Categorie = mysql_fetch_assoc($Categorie);
$totalRows_Categorie = mysql_num_rows($Categorie);

mysql_select_db($database_locale, $locale);
$query_Abbonato = "SELECT * FROM abbonati";
$Abbonato = mysql_query($query_Abbonato, $locale) or die(mysql_error());
$row_Abbonato = mysql_fetch_assoc($Abbonato);
$totalRows_Abbonato = mysql_num_rows($Abbonato);

mysql_select_db($database_locale, $locale);
$query_Citta = "SELECT * FROM citta order by nome_citta";
$Citta = mysql_query($query_Citta, $locale) or die(mysql_error());
$row_Citta = mysql_fetch_assoc($Citta);
$totalRows_Citta = mysql_num_rows($Citta);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../style.css" rel="stylesheet" type="text/css">


<link rel="stylesheet" type="text/css" href="../epoch_styles.css" />
<script type="text/javascript" src="../jscripts/epoch_classes.js"></script>
<script type="text/javascript" src="../jscripts/saveform.js"></script>


</head>

<body>
<fieldset>
<legend>Inserimento Abbonato</legend>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="left">
    <tr> 
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Nominativo:</strong></td>
      <td><input type="text" name="nominativo" value="" size="32"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Riferimento:</strong></td>
      <td><input type="text" name="riferimento" value="" size="32"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Indirizzo:</strong></td>
      <td><input type="text" name="indirizzo" value="" size="50"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Cap:</strong></td>
      <td><input type="text" name="cap" value="" size="6"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Comune:</strong></td>
      <td> <select name="id_citta">
          <?php 
do {  
?>
          <option value="<?php echo $row_Citta['id_citta']?>" ><?php echo $row_Citta['nome_citta']?></option>
          <?php
} while ($row_Citta = mysql_fetch_assoc($Citta));
?>
        </select> </td>
    <tr> 
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Data Reg.</strong></td>
      <td><input type="text" name="data_registrazione" id="data_registrazione" value="" size="10">
	  
	  <script language="JavaScript" type="text/javascript">
		var dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('data_registrazione'));
	</script>
	  
	  </td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Categoria:</strong></td>
      <td> <select name="id_categoria">
          <?php 
do {  
?>
          <option value="<?php echo $row_Categorie['id_categoria']?>" ><?php echo $row_Categorie['descrizione_categoria']?></option>
          <?php
} while ($row_Categorie = mysql_fetch_assoc($Categorie));
?>
        </select> </td>
    <tr> 
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Telefono:</strong></td>
      <td><input type="text" name="telefono" value="" size="20"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Cellulare:</strong></td>
      <td><input type="text" name="cellulare" value="" size="20"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right"><strong>Email:</strong></td>
      <td><input type="text" name="email" value="" size="32"></td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right" valign="top"><strong>Note:</strong></td>
      <td> <textarea name="note" cols="50" rows="5"></textarea> 
      </td>
    </tr>
    <tr valign="baseline"> 
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Inserisci Abbonato"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>

</fieldset>
<p><a href="elenco_abbonati.php">&lt;- Torna a Elenco Abbonati</a> </p>
</body>
</html>
<?php
mysql_free_result($Categorie);

mysql_free_result($Abbonato);

mysql_free_result($Citta);
?>
